package com.example.liwushuo;

import java.util.ArrayList;
import java.util.List;

import com.example.liwushuo.adapter.GridViewInfoAdapter;
import com.example.liwushuo.adapter.HorAdapter;
import com.example.liwushuo.bean.BaseBean;
import com.example.liwushuo.bean.Item;
import com.example.liwushuo.bean.Posts;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class GVInfoActivity extends ActionBarActivity {

	private ListView listView;
	private GridViewInfoAdapter adapter;
	private List<Item> list;
	HttpUtils httpUtils = new HttpUtils();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gvinfo);
		initView();
		initData();
	}
	private void initView() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.gv_listview);
		list = new ArrayList<Item>();
		adapter = new GridViewInfoAdapter(list, getApplicationContext());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String url = list.get(arg2).getUrl();
				Intent intent = new Intent(getApplicationContext(),
						HorInfoActivity.class);
				intent.putExtra("url", url);

				startActivity(intent);
			}
		});
	}

	public static String getGoodsInfoTallUrl(int id) {

		return "http://api.liwushuo.com/v2/channels/"+id+"/items?limit=20&offset=0&gender=2&generation=2";
	}
	private void initData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		int id = intent.getIntExtra("id", 1);
		httpUtils.send(HttpMethod.GET, getGoodsInfoTallUrl(id),
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub
						String json = responseInfo.result;

						BaseBean baseBean = com.alibaba.fastjson.JSONObject
								.parseObject(json, BaseBean.class);

						List<Item> items2 = baseBean.getData().getItems();

						list.addAll(items2);

					}

				});
	}

	

}
